﻿using System;
using UnityEngine;
using System.Collections.Generic;

namespace Map.Data
{
    /// <summary>
    /// 地图节点类
    /// </summary>
    public class MapNode : MonoBehaviour
    {
        private MapPoint3D gridPosition = MapPoint3D.Zero;
        private bool _activating = false;
        public MapPoint3D GridPosition
        {
            get { return gridPosition; }
        }
        private Dictionary<MapNodeDirectionType, MapNode> _nodes = new Dictionary<MapNodeDirectionType, MapNode>();
        #region 设置节点关系函数
        /// <summary>
        /// 设置上方第一个节点
        /// </summary>
        /// <param name="targetNode"></param>
        public void SetUp0Node(MapNode targetNode)
        {
            targetNode[MapNodeDirectionType.Down1] = this;
            this[MapNodeDirectionType.Up0] = targetNode;
        }
        /// <summary>
        /// 设置下方第第二个节点
        /// </summary>
        /// <param name="targetNode"></param>
        public void SetDown1Node(MapNode targetNode)
        {
            targetNode[MapNodeDirectionType.Up0] = this;
            this[MapNodeDirectionType.Down1] = targetNode;
        }
        /// <summary>
        /// 设置上方第二个节点
        /// </summary>
        /// <param name="targetNode"></param>
        public void SetUp1Node(MapNode targetNode)
        {
            targetNode[MapNodeDirectionType.Down0] = this;
            this[MapNodeDirectionType.Up1] = targetNode;
        }
        /// <summary>
        /// 设置下方第一个节点
        /// </summary>
        /// <param name="targetNode"></param>
        public void SetDown0Node(MapNode targetNode)
        {
            targetNode[MapNodeDirectionType.Up1] = this;
            this[MapNodeDirectionType.Down0] = targetNode;
        }
        /// <summary>
        /// 设置左边节点
        /// </summary>
        /// <param name="targetNode"></param>
        public void SetLeftNode(MapNode targetNode)
        {
            targetNode[MapNodeDirectionType.Right] = this;
            this[MapNodeDirectionType.Left] = targetNode;
        }
        /// <summary>
        /// 设置右边节点
        /// </summary>
        /// <param name="targetNode"></param>
        public void SetRightNode(MapNode targetNode)
        {
            targetNode[MapNodeDirectionType.Left] = this;
            this[MapNodeDirectionType.Right] = targetNode;
        }
        #endregion
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="indexX">二维数组中X的下标值</param>
        /// <param name="indexY">二维数组中Y的下标值</param>
        public void Init(int indexX, int indexY)
        {
            gridPosition = new MapPoint3D(indexX, indexY);
            var types = Enum.GetValues(typeof(MapNodeDirectionType));
            _nodes.Clear();
            foreach (var type in types)
            {
                _nodes.Add((MapNodeDirectionType)type, null);
            }
        }
        public MapNode this[MapNodeDirectionType index]
        {
            get
            {
                return _nodes[index];
            }
            private set { _nodes[index] = value; }
        }
        // Use this for initialization
        void Start()
        {
            GetComponent<Renderer>().material.color = new Color(0.5f,0.5f,0.5f);
        }
        // Update is called once per frame
        void Update()
        {

        }
        public void OnMouseEnter()
        {
            GetComponent<Renderer>().material.color = new Color(0, 0.5f, 1);
        }
        public void OnMouseExit()
        {
            GetComponent<Renderer>().material.color = _activating? new Color(0,0.5f,1f):new Color(0.5f,0.5f,0.5f);
        }
        public void OnMouseDown()
        {
            _activating = !_activating;
        }
    }
}
